package avicit.im.lantop.basic.companycorrespondence.action;

import avicit.im.lantop.basic.companycorrespondence.service.CompanyCorrespondenceService;
import avicit.im.lantop.basic.companycorrespondence.service.api.CompanyCorrespondenceServiceApi;
import avicit.platform6.api.session.SessionHelper;
import avicit.platform6.api.sysuser.SysOrgAPI;
import avicit.platform6.api.sysuser.dto.SysOrg;
import avicit.platform6.api.sysuser.dto.SysUser;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.properties.PlatformConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CompanyCorrespondenceController
 * @Description 新旧单位部门对应
 * @Author zcl
 * @Date 2020/9/16 9:57
 */
@Controller
@SuppressWarnings({"deprecation"})
@RequestMapping("/CompanyCorrespondenceController")
public class CompanyCorrespondenceController {

    @Autowired
    private CompanyCorrespondenceServiceApi companyCorrespondenceService;

    @Autowired
    private SysOrgAPI sysOrgAPI;


    /**
     * 跳转部门对应页面
     * @param request
     * @return
     */
    @RequestMapping("/toCompanyCorrespondenceJsp")
    public ModelAndView toCompanyCorrespondenceJsp(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        SysUser sysUser = SessionHelper.getLoginSysUser(request);
        String createdBy = sysUser.getName();
        mav.addObject("dialogId", request.getParameter("dialogId"));
        mav.addObject("createdName", createdBy);
        mav.setViewName("avicit/im/lantop/basic/companycorrespondence/LantopCompanyCorrespondenceAdd");
        return mav;
    }

    /**
     * 加载部门对应页面数据
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/loadCompanyCorrespondence")
    @ResponseBody
    public Map<String,Object> loadCompanyCorrespondence(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String,Object> map = new HashMap<>();
        String orgId = ThreadContextHelper.getOrgId(); //ORGID
        String pageNo = request.getParameter("page");// 取得当前页码
        String pageSize = request.getParameter("rows");// 取得一页显示的数量
        Paging<Map<String, Object>> page = new Paging<Map<String, Object>>();
        page.setPageSize(ComUtil.getPageSize(pageSize));
        page.setPageNo(ComUtil.getPageNo(pageNo));
        Map<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("orgId", orgId);
        Page<Map<String, Object>> result = companyCorrespondenceService.loadCompanyCorrespondence(page, parameter);
        map.put("total", result.getEntityCount()); // total键
        map.put("rows", result.getEntities()); // rows键 存放每页记录 list
        return map;
    }

    /**
     * 删除
     * @param request
     * @return
     */
    @RequestMapping({"/deleteCompanyCorrespondence"})
    @ResponseBody
    public Map<String,Object> deleteCompanyCorrespondence(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        try {
            String ids = request.getParameter("ids");
            if ((ids != null) && (!"".equals(ids))) {
                this.companyCorrespondenceService.deleteCompanyCorrespondence(ids);
                map.put("flag", PlatformConstant.OpResult.success);
            }
        } catch (Exception e) {
            map.put("flag", PlatformConstant.OpResult.failure);
        }
        return map;
    }

    /**
     * 新增
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("insertCompanyCorrespondence")
    @ResponseBody
    public Map<String,Object> insertCompanyCorrespondence(HttpServletRequest request) throws Exception {
        Map<String,Object> map = new HashMap<>();
        String orgId = ThreadContextHelper.getOrgId();
        String currentDeptId = request.getParameter("currentDeptId");
        String currentDeptName = request.getParameter("currentDeptName");
        String counterpartDeptNames = request.getParameter("counterpartDeptNames");
        try {
            if(StringUtils.isNotBlank(counterpartDeptNames)){
                String deptNames[] = counterpartDeptNames.split(",");
                for (int i = 0; i < deptNames.length; i++) {
                    Boolean check = companyCorrespondenceService.queryCheckData(currentDeptId, deptNames[i]);
                    if(!check){
                        this.companyCorrespondenceService.insertCompanyCorrespondence(orgId, currentDeptId, currentDeptName, deptNames[i]);
                    }
                }
                map.put("flag", PlatformConstant.OpResult.success.ordinal());
            }else{
                map.put("flag", PlatformConstant.OpResult.failure.ordinal());
            }
        } catch (Exception e) {
            map.put("flag", PlatformConstant.OpResult.failure.ordinal());
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取新旧单位对应部门
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("getDept")
    @ResponseBody
    public Map<String,Object> getDept(HttpServletRequest request) throws Exception {
        Map<String,Object> map = new HashMap<>();
        String orgId = ThreadContextHelper.getOrgId();
        SysOrg so = sysOrgAPI.getSysOrgBySysOrgId(orgId);
        String inputWord = request.getParameter("inputWord");
        String orgCode = so.getOrgCode();
        Map<String, Object> result = companyCorrespondenceService.getDept(orgId, orgCode,inputWord);
        map.put("rows", result);
        return map;
    }

    /**
     *新增页面，选择现有部门时，需要根据已保存的对应关系，再剔除旧部门中已对应的部门
     * @param request
     * @return
     */
    @RequestMapping("getOldDept")
    @ResponseBody
    public Map<String,Object> getOldDept(HttpServletRequest request) throws Exception {
        Map<String,Object> map = new HashMap<>();
        String orgId = ThreadContextHelper.getOrgId();
        String newDeptName = request.getParameter("newDeptName");
        String inputWord = request.getParameter("inputWord");
        SysOrg so = sysOrgAPI.getSysOrgBySysOrgId(orgId);
        String orgCode = so.getOrgCode();
        Map<String, Object> result = companyCorrespondenceService.getOldDept(inputWord,newDeptName,orgId,orgCode);
        map.put("rows", result);
        return map;
    }
}
